#===============================================
# Code written by Joe France (joeprogram.com)
# Released under code.google.com/p/video-scholar/
# with the MIT license (aka, the do-whatever-you-want-with-it license)
#===============================================

import pyglet
from lxml import etree
from CompoundGUI import CompoundGUI
from GUI import GUI

class HotKey( CompoundGUI ):

	SPACE_BETWEEN_KEY_AND_LABEL = 50
	
	def __init__( self, key, label = 'Good', color = (255,0,0), ):
	
		super( HotKey, self).__init__()
	
		self.key = key
		self.mark_group = None
		
		self.key_gui = GUI( 'hot_key.png', color = color )
		self.key_gui.set_coordinates( x = self.x, y = self.y )
		
		self.label = pyglet.text.Label(
			label,
			font_name = 'Trebuchet MS',
			font_size = 24,
			x = self.x + self.SPACE_BETWEEN_KEY_AND_LABEL, 
			y = self.y,
			anchor_x='left', 
			anchor_y = 'bottom',
		)
		
		self.key_label = pyglet.text.Label(
			pyglet.window.key.symbol_string(key),
			font_name = 'Trebuchet MS',
			font_size = 20,
			x = self.key_gui.x + self.key_gui.get_width()/2,
			y = self.key_gui.y + self.key_gui.get_height()/2,
			anchor_x = 'center',
			anchor_y = 'center',
		)
			
		self.guis = [ self.key_gui ]
		
	@classmethod					
	def create_from_xml( cls, node ):
		'''
		An alternative constructor
		Where you provide a valid hotkey node
		And it creates the object and returns it
		'''
		
		return HotKey(
			key = int(node.get('key')),
			label = node.get('label'),
			color = (
				int(node.get('red')),
				int(node.get('green')),
				int(node.get('blue')),
			),
		)
		
	def set_mark_group( self, mark_group ):
		'''
		After the hotkey is initiallized,
		It needs to wait for the Timeline to set up the Mark Groups
		Each Mark Group will then associate itself with a hotkey.
		'''
		self.mark_group = mark_group
		
	def set_coordinates( self, x = None, y = None ):
		'''
		Since the position of a HotKey is dependent on how many exist,
		The owner needs to set the position after creation
		'''
			
		self.key_gui.set_coordinates(x,y)
		
		if x is not None:
			self.label.x = x + self.SPACE_BETWEEN_KEY_AND_LABEL
			self.key_label.x = x + self.key_gui.get_width()/2
		if y is not None:
			self.label.y = y
			self.key_label.y = y + self.key_gui.get_height()/2
		
		
	def get_color( self ):
		return self.key_gui.color
		
	def get_base_color( self ):
		return self.key_gui.base_color
		
	def get_key( self ):
		return self.key
		
	def draw( self ):
	
		self.key_gui.draw()
		self.label.draw()
		self.key_label.draw()
		
	def key_press( self, symbol, modifiers ):
		'''
		This hotkey listens for its key
		If its pressed, it sends a command off to its mark group
		To add a new mark
		'''
		
		if self.key == symbol and self.mark_group:
			self.mark_group.add_mark()
			
	def mouse_press( self, x, y, button, modifiers ):
		self.mark_group.add_mark()
			
	def export_xml( self, node ):
	
		key_node = etree.SubElement(node,'hot_key')
		key_node.set('key',str(self.key) )
		key_node.set('label',self.label.text )
		key_node.set('red',str(self.key_gui.color[0]))
		key_node.set('green',str(self.key_gui.color[1]))
		key_node.set('blue',str(self.key_gui.color[2]))
		